using UnityEngine;
using UnityEngine.UI;
using EchoFramework;

/// <summary>
/// 通关界面
/// </summary>
public class VictoryView : UIViewBase
{
    private Text stageClearTxt;
    private Text victoryTxt;
    private Button closeBtn;
    private Button nextLevelBtn;
    private Button homeBtn;
    private Button restartBtn;
    private Button exitBtn;

    public override void OnAwake()
    {
        base.OnAwake();
        stageClearTxt = prefabBinder.Find("StageClearTxt").GetComponent<Text>();
        victoryTxt = prefabBinder.Find("VictoryTxt").GetComponent<Text>();
        closeBtn = prefabBinder.Find("CloseBtn").GetComponent<Button>();
        closeBtn.onClick.AddListener(OnClickCloseBtn);
        nextLevelBtn = prefabBinder.Find("NextLevelBtn").GetComponent<Button>();
        nextLevelBtn.onClick.AddListener(OnClickNextLevelBtn);
        homeBtn = prefabBinder.Find("HomeBtn").GetComponent<Button>();
        homeBtn.onClick.AddListener(OnClickHomeBtn);
        restartBtn = prefabBinder.Find("RestartBtn").GetComponent<Button>();
        restartBtn.onClick.AddListener(OnClickRestartBtn);
        exitBtn = prefabBinder.Find("ExitBtn").GetComponent<Button>();
        exitBtn.onClick.AddListener(OnClickExitBtn);
    }

    public override void OnStart(params object[] objs)
    {
        base.OnStart(objs);
        int level = MapLogicModule.Instance.Level;
        int maxLevel = MapLogicModule.Instance.MaxLevel;
        nextLevelBtn.gameObject.SetActive(level < maxLevel);
        homeBtn.gameObject.SetActive(level == maxLevel);
        stageClearTxt.gameObject.SetActive(level == maxLevel);
        victoryTxt.gameObject.SetActive(level < maxLevel);
        AudioManager.Instance.PlayVictoryClip();
    }

    /// <summary>
    /// 点击关闭按钮
    /// </summary>
    private void OnClickCloseBtn()
    {
        AudioManager.Instance.PlayNormalBtnClip();
        CloseWindow();
    }

    /// <summary>
    /// 点击前往下一关按钮
    /// </summary>
    private void OnClickNextLevelBtn()
    {
        AudioManager.Instance.PlayNormalBtnClip();
        if(MapLogicModule.Instance.Level < MapLogicModule.Instance.MaxLevel)
            MapLogicModule.Instance.LoadMapLevel(MapLogicModule.Instance.Level + 1);
        
        CloseWindow();
    }

    /// <summary>
    /// 点击首页按钮
    /// </summary>
    private void OnClickHomeBtn()
    {
        AudioManager.Instance.PlayNormalBtnClip();
        UIViewManager.Instance.OpenWindow(UIViewID.MainMenuView);
        CloseWindow();
    }

    /// <summary>
    /// 点击重开按钮
    /// </summary>
    private void OnClickRestartBtn()
    {
        AudioManager.Instance.PlayNormalBtnClip();
        MapLogicModule.Instance.LoadMapLevel(MapLogicModule.Instance.Level);
        CloseWindow();
    }

    /// <summary>
    /// 点击退出按钮
    /// </summary>
    private void OnClickExitBtn()
    {
        AudioManager.Instance.PlayNormalBtnClip();
        UIViewManager.Instance.OpenWindow(UIViewID.SelectLevelView);
        CloseWindow();
    }
}